#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn = 100005;
const int maxm = 200005;
int n,m,num_edge;
int fa[maxn];
long long ans;
 
struct node{
    int from;
    int next;
    int dis;
    bool operator<(const node &b) const
    { return dis < b.dis; }
}edge[maxm];

void add_edge(int from,int to,int dis){
    edge[++num_edge].from=from;
    edge[num_edge].dis=dis;
    edge[num_edge].next=to;
}

int findf(int x)
{
    return fa[x] == x ? x : fa[x] = findf(fa[x]);
}

bool join(const int &u, const int &v)
{
    int f1 = findf(u), f2 = findf(v);
    if (f1 == f2) return false;
    else { fa[f2] = f1; return true; }
}



void kru(){
    int cnt=0;
    for(int i=0;i<=n;i++)fa[i]=i;
    sort(edge+1,edge+1+num_edge);
    for(int i=1;i<=num_edge;i++){
        const int &u=edge[i].from;
        const int &v=edge[i].next;
        if(join(u,v)){
            cnt++;
            ans+=edge[i].dis;
            if(cnt>=n)return;
        }
    }
}

int main(){
    cin>>n;
    cin>>m;
    int a,b,c;

    for(int i=1;i<=m;i++){
        cin>>a>>b>>c;
        add_edge(a,b,c);
    }
    kru();
    
    cout<<ans<<endl;
    
}
